{header}
<link rel="stylesheet" type="text/css" href="/static/plugins/webuploader-0.1.5/webuploader.css">
<script type="text/javascript" src="/static/plugins/webuploader-0.1.5/webuploader.min.js"></script>
<div class="card">
  <div class="card-header">
    操作
  </div>
  <div class="card-body">
    <div class="form-group">
      <label>键</label>
      <input type="text" class="form-control" id="global_key">
    </div>
    <div class="form-group">
      <label>值</label>
      <input type="text" class="form-control" id="global_value">
    </div>
    <button type="button" class="btn btn-primary" id="save">一键保存</button>
  </div>
</div>
<form>
  {tr}
  <script type="text/javascript">
    var globals = [];
    $(function () {
      {array_js}
      $('#save').on('click', function () {
        var global_key = $('#global_key').val();
        var global_value = $('#global_value').val();
        if (global_key === '' && global_value !== '') {
          layer.alert('值不为空则键也不能为空', {icon: 0});
          return;
        } else if (global_key.indexOf('\'') !== -1 || global_key.indexOf('"') !== -1 || global_key.indexOf('\\') !== -1 ||
          global_value.indexOf('\'') !== -1 || global_value.indexOf('"') !== -1 || global_value.indexOf('\\') !== -1) {
          layer.alert('表单不能含有引号和斜杠', {icon: 0});
          return;
        }
        for (var i in globals) {
          var v = globals[i];
          var value = $('#global' + v['index']).val();
          if (value.indexOf('\'') !== -1 || value.indexOf('"') !== -1 || value.indexOf('\\') !== -1) {
            layer.alert('表单不能含有引号和斜杠', {icon: 0});
            return;
          }
          if (global_key === v['key']) {
            layer.alert('键不能有重复', {icon: 0});
            return;
          }
        }
        var g_str = '';
        if (global_key !== '') {
            g_str = '0\'' + global_key + '\'' + global_value;
        }
        for (var i in globals) {
          var v = globals[i];
          if (g_str === '') {
            g_str = v['id'] + '\'' + v['key'] + '\'' + $('#global' + v['index']).val();
          } else {
            g_str += '"' + v['id'] + '\'' + v['key'] + '\'' + $('#global' + v['index']).val();
          }
        }
        var save = $('#save');
        save.attr('disabled', true);
        $.ajax({
          url: '{base_url}library/upload_global_vars',
          type: 'post',
          data: {
            g_str: g_str
          },
          success: function (data) {
            var d = data.split(':');
            save.attr('disabled', false);
            if (d.length !== 2) {
              console.error(data);
              layer.alert('服务器返回非法数据', {icon: 2});
              return;
            }
            if (d[0] === 'fail') {
              layer.alert('提交失败:' + d[1], {icon: 2});
              console.error('提交失败:' + d[1]);
            } else if (d[0] === 'success') {
              location.reload();
            } else {
              layer.alert('服务器返回多余数据', {icon: 2});
            }
          },
          error: function (data) {
            layer.alert('上传失败', {icon: 2});
            console.error(data.responseText);
          }
        });
      });
    });
  </script>
</form>
{footer}

